// components/article-item/index.js
import {
  like,
  cancelLike,
  collect,
  cancelCollect
} from '../../api/ChatTopicController'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    item: {
      type: Object
    },
    userId: {
      type: String
    }
  },
  lifetimes: {
    attached() {
      this.video = wx.createVideoContext("video" + this.data.item.topicId, this);
      this.checkFold()
    }
  },
  //添加判断划出屏幕就不播放
  ready: function () {
    this.createIntersectionObserver().relativeToViewport().observe("#" + "video" + this.data.item.topicId, (res) => {
      if (res.intersectionRatio > 0.03 && this.data.attr.autoPlay) {
        this.video.play()
      } else {
        this.video.pause()
      }
    })
  },
  /**
   * 组件的初始数据
   */
  data: {
    view: false, //是否折叠文本
    text: false, //是否显示折叠操作
    playId: null
  },

  /**
   * 组件的方法列表
   */
  methods: {

    //rpx转换px
    changeRpxToPx(rpxInteger) {
      return wx.getSystemInfoSync().windowWidth / 750 * rpxInteger
    },
    //查看全文
    checkFold() {
      const query = this.createSelectorQuery();
      query.select(".content-all-text").boundingClientRect(res => {
        const lineHeightToPx = this.changeRpxToPx(40);
        if (res.height > lineHeightToPx * 2) {
          this.setData({
            view: true,
            text: true
          })
        }
      }).exec()
    },
    viewText() {
      this.setData({
        view: !this.data.view
      })
    },
    //查看图片
    viewImage(ev) {
      wx.previewImage({
        urls: ev.mark.urls,
        current: ev.mark.urls[ev.target.dataset.index]
      })
    },
    //帖子操作
    async topicStatus(ev) {
      const userId = wx.getStorageSync('userId')
      if (!userId) return wx.utils.toast('请先登录')
      const type = ev.mark.type
      const FUpperType = 'has' + type.charAt(0).toUpperCase() + type.slice(1)
      const {
        topicId,
      } = this.properties.item
      if (this.properties.item[FUpperType] === '0') {
        const res = type === 'like' ? await like({
          topicId,
          userId
        }) : await collect({
          topicId,
          userId,
          content: this.properties.item.content,
          file: this.properties.item.file.length !== 0 ? this.properties.item.file : this.properties.item.video,
          title: this.properties.item.title
        })
        const count = type + 'Count'
        this.triggerEvent("updateStatus", {
          topicId,
          type,
          has: '1',
          count: +this.properties.item[count] * 1 === this.properties.item[count] ? +this.properties.item[count] + 1 : this.properties.item[count]

        })
      } else {
        const res = type === 'like' ? await cancelLike({
          topicId,
          userId
        }) : await cancelCollect({
          topicId,
          userId
        })

        const count = type + 'Count'
        this.triggerEvent("updateStatus", {
          topicId,
          type,
          has: '0',
          count: +this.properties.item[count] * 1 === this.properties.item[count] ? +this.properties.item[count] - 1 : this.properties.item[count]
        })

      }

    },
    //播放视频
    play(ev) {
      this.triggerEvent("updatePlayId", {
        id: ev.mark.id,
        _this: this
      })
    },
    //去帖子详情
    toTopicDetail(ev) {
      // if (!wx.getStorageSync('userId')) return wx.utils.toast('请先登录')
      if (ev.mark.type) return wx.navigateTo({
        url: `/pkgPublish/pages/topicDetail/index?topicId=${this.properties.item.topicId}&type=${ev.mark.type}`,
      })
      wx.navigateTo({
        url: `/pkgPublish/pages/topicDetail/index?topicId=${this.properties.item.topicId}`,
      })
    },
    //去私聊页面
    toChat() {
      if (!wx.getStorageSync('userId')) return wx.utils.toast('请先登录')
      wx.navigateTo({
        url: `/pkgMessage/pages/chat/index?userId=${this.properties.item.userId}&nickName=${this.properties.item.nickName}`,
      })
    },
    //去主页
    toOtherPersonalHome(e) {
      wx.navigateTo({
        url: '/pkgUser/pages/otherPersonalHome/index?userId=' + e.mark.userId,
      })
    },
  }
})